package DAO;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import Model.Hospitalizacao;
import Model.Paciente;
import Utils.Conexao;

import com.mysql.jdbc.CallableStatement;

public class HospitalizacaoDAO {
	public static void Insert(Hospitalizacao hosp) throws SQLException
	{
		Connection con = Conexao.getConection();		
		String mysqlCall = "call spc_insert_hospitalizacao(?, ?, ?, ?, ?)";			
		CallableStatement call = HospitalizacaoDAO.setObjectCallableStatement(hosp, mysqlCall, con);
		call.executeUpdate();
		con.close();		
		call.close();
	}
	
	public static void Update(Hospitalizacao hosp) throws SQLException
	{		
		Connection con = Conexao.getConection();		
		String mysqlCall = "call spc_update_hospitalizacao(?, ?, ?, ?, ?, ?)";		
		CallableStatement call = HospitalizacaoDAO.setObjectCallableStatement(hosp, mysqlCall, con) ;
		call.executeUpdate();
		con.close();
		call.close();		
	}
	
	public static void inativarHospitalizacao(int idHospitalizacao) throws SQLException
	{		
		Connection con = Conexao.getConection();		
		String mysqlCall = "call spc_inativar_hospitalizacao(?)";		
		CallableStatement call = (CallableStatement)con.prepareCall(mysqlCall);
		call.setInt(1, idHospitalizacao);
		call.execute();
		con.close();
		call.close();		
	}	
	
	public static Hospitalizacao LoadObject(int idHospitalizacao) throws SQLException
	{		
		Hospitalizacao hospitalizacao = new Hospitalizacao();
		Connection con = Conexao.getConection();		
		String mysqlCall = "call spc_select_hospitalizacao_por_id(?)";
		CallableStatement call =(CallableStatement) con.prepareCall(mysqlCall);
		call.setInt(1, idHospitalizacao);
		
		ResultSet res = call.executeQuery();
		
		if(res.next())
			hospitalizacao = HospitalizacaoDAO.setInstance(res);		
		
		con.close();		
		call.close();	
		
		return hospitalizacao;
	}	
	
	public static ArrayList<Hospitalizacao> GetArrayListHospitalizacao()
	{
		Connection con = Conexao.getConection();		
		ArrayList<Hospitalizacao> arrhospitalizacao = new ArrayList<Hospitalizacao>();
		Hospitalizacao hospitalizacao = null;
		try
		{
			String mysqlCall = "call spc_select_hospitalizacoes()";
			CallableStatement call =(CallableStatement) con.prepareCall(mysqlCall);
			ResultSet res = call.executeQuery();			
			
			while(res.next())
			{
				hospitalizacao =  HospitalizacaoDAO.setInstance(res);
				arrhospitalizacao.add(hospitalizacao);
			}			
			
			call.close();
			con.close();
		}
		catch(SQLException ex)
		{
			ex.printStackTrace();
		}
		
		return arrhospitalizacao;
	}
	
	private static CallableStatement setObjectCallableStatement(Hospitalizacao hosp, String mysqlCall, Connection con ) throws SQLException
	{	
		CallableStatement call = (CallableStatement) con.prepareCall(mysqlCall);
		call.setString(1, hosp.getNomeHospital());
		call.setDate(2, hosp.getDataInternacao());
		call.setDate(3, hosp.getDataAlta());
		call.setString(4,   hosp.getObservacao());
		call.setInt(5, hosp.getIdPaciente());
		
		if(hosp.getIdHospitalizacao() > 0)
			call.setInt(6, hosp.getIdHospitalizacao());
	
		return call; 		
	}
	
	public static Hospitalizacao setInstance(ResultSet res) throws SQLException
	{
		Hospitalizacao hospitalizacao = new Hospitalizacao(); 		
		hospitalizacao.setIdHospitalizacao(res.getInt("id_hospitalizacao"));
		hospitalizacao.setNomeHospital(res.getString("nome_hospital"));
		hospitalizacao.setDataInternacao(res.getDate("data_internacao"));
		hospitalizacao.setDataAlta(res.getDate("data_alta"));
		hospitalizacao.setObservacao(res.getString("observacao"));
		hospitalizacao.setIdPaciente(res.getInt("id_paciente"));
		hospitalizacao.setPaciente(PacienteDAO.LoadObject(res.getInt("id_paciente")));
		return hospitalizacao;		
	}
	
}
